package com.smasher.doc.util;

import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.util.Log;

import androidx.activity.result.IntentSenderRequest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class CopyUtil {

    public static final String TAG = "CopyUtil";

    /**
     * 拷贝沙盒中的文件到外部存储区域
     *
     * @param filePath    沙盒文件路径
     * @param externalUri 外部存储文件的 uri
     */
    public static boolean copySandFileToExternalUri(
            ContentResolver contentResolver, String filePath, Uri externalUri
    ) {
        boolean ret = false;
        try (OutputStream outputStream = contentResolver.openOutputStream(externalUri)) {
            File sandFile = new File(filePath);
            if (sandFile.exists()) {
                try (InputStream inputStream = new FileInputStream(sandFile)) {
                    int readCount = 0;
                    byte[] buffer = new byte[1024];
                    while ((readCount = inputStream.read(buffer)) != -1) {
                        if (outputStream != null) {
                            outputStream.write(buffer, 0, readCount);
                            outputStream.flush();
                        }
                    }
                    ret = true;
                } catch (FileNotFoundException e){
                    Log.e(TAG, "copy SandFile To ExternalUri. e = " + e.toString());
                }
            }
        } catch (IOException e) {
            Log.e(TAG, "copy SandFile To ExternalUri. e = " + e.toString());
        }
        return ret;
    }
}
